#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include "../include/meta.h"


int hash(char *filename)
{
	char c;
	c = filename[0];

	int num = c;
	
	if(num>=48 && num<=57)
	{
		return (num+2)%50;

	}

	if(num>= 97 && num<=122)
	{
		return ((num+3)%100)+10;
	}

	if(num>=65 && num<=90)
	{
		return ((num+5)%70)+36;
	}
	
return 62;
	
}

void display()
{
	printf("in display\n");
	int i=0;
	struct list *temp;
	for(i=0;i<63;i++)
	{
		while(hash_table[i]!=NULL)
		{	
			temp = hash_table[i];
			while(temp!=NULL)
			{
				printf("%s\t", temp->filename);
				printf("%s\t", temp->pathname);
				temp=temp->next;
			}
			printf("\n");
			i++;
			if(i==63)
				break;
		}
	}
}


struct list* srcFile(int index, char name_File[])
{	
      struct list *start,*fresh, *fresh_head,*fresh_temp;
	
	fresh_head= (struct list *)malloc(sizeof(struct list));
	fresh_temp=fresh_head;
      start=hash_table[index];	
	while(start!=NULL)
	{	if (strcmp(start->filename,name_File)==0)
		{	fresh= (struct list *)malloc(sizeof(struct list));
			strcpy(fresh->filename,start->filename); 
			strcpy(fresh->pathname, start->pathname); 
			fresh->next=NULL;

			fresh_temp->next=fresh;
			fresh_temp=fresh_temp->next;
			                     
		}
		//printf("%s ",fresh->pathname);
		start = start->next;
	}

return fresh_head->next;

}



struct list* serchStartPoint(char *name_File)
{	int index;
         index = hash(name_File);
 	struct list* temp= (struct list *)malloc(sizeof(struct list));
	if(hash_table[index] == NULL)
		printf("0 Files Found. Search Ended...\n");	
	else if(index == -1)
		printf("Invalid file name\n");
	else
		{temp= srcFile(index,name_File); return temp; }
return NULL;
}



void insert_into_list(int hashvalue, char file[],char path[])
{
	struct list *node;
	node = (struct list *)malloc(sizeof(struct list));

	strcpy(node->filename, file);
	strcpy(node->pathname, path);
	node->next = NULL;

	if(hash_table[hashvalue]== NULL)
		hash_table[hashvalue]= node;

	else
	{
		struct list *temp;
		temp = hash_table[hashvalue];

		while(temp->next!=NULL)
			temp = temp->next;

		temp->next = node;	
	}
	printf("display after insert");
	
} 





int deletion(int hashvalue, char *f,char *p)
{	
 printf("display b4 delete");
	
	struct list * temp1, *temp2,*del;
	temp1 = hash_table[hashvalue];
	temp2 = hash_table[hashvalue];
	
	if(temp1==NULL)
                {
                  printf("no entry to delete");
                  return 0;
                }

	if(strcmp(temp1->filename,f)==0 && strcmp(temp1->pathname,p)==0)
	{
           printf("%s is deleted",f,p);
              
		temp2= temp1->next;
		hash_table[hashvalue]=temp2;
		free(temp1);
		return 1;
	}
	else
	{	printf("in else");
		while(temp1!= NULL)
		{
	
			if(strcmp(temp1->filename,f)==0 && strcmp(temp1->pathname,p)==0)
			{       
				printf("%s is deleted from %s",f,p);
				    
					temp2->next=temp1->next;
					del=temp1;
                                        temp1=temp1->next;
					free(del);
				
			}
			else
			{
				temp2 = temp1;					
				temp1 = temp1->next;
				
			}
		} return 1;
	}
}

struct list* search_files(char *start_filesname)
{       int index,length;
        struct list *start,*fresh,*temp,*head;
        head=(struct list *)malloc(sizeof(struct list));
        temp=head;
        index = hash(start_filesname);
        start=hash_table[index];
        length=strlen(start_filesname);
        if(hash_table[index] == NULL)
        {
                return NULL;
        }
        else if(index == -1)
        {
                return NULL;
        }
        else{
                        while(start!=NULL)
                {      
                        if (strncmp(start->filename,start_filesname,length)==0)
                           {    
                                fresh= (struct list *)malloc(sizeof(struct list));
                                strcpy(fresh->pathname,start->pathname);  
                                strcpy(fresh->filename,start->filename);                
                                fresh->next=NULL;
                                temp->next=fresh;
                                temp=fresh;              
                           }
                        start = start->next;
                }
               
        }
          return head->next;
}

/*
int main()
{
	int i;

	for(i=0;i<=61;i++)
	{
		//hash_table[i] = (struct list *)malloc(sizeof(struct list));
		hash_table[i] = NULL;
	}

	char file[30], f[30],p[30],search_file[30],path[30],prefix_files[30];
	int hashvalue=0;
	int option, choice;

	while(1)
	{
		printf("\nMENU\n");
		printf("1-Display\n");
		printf("2-Insert a file into hash table\n");
		printf("3-Search for a file in the hash table\n");
                printf("4-Delete a file from the hash \n");
		printf("5-Enter the prefix of the files to be searched \n");
		printf("6-Exit\n");

		printf("Enter your choice\n");
		scanf("%d", &choice);

		switch(choice)
		{
			case 1: display();
					break;

			case 2: printf("\nEnter the file name to be inserted into the hash table\n");
					scanf("%s", file);
					printf("\nEnter the path of the file\n");
					scanf("%s", path);
					hashvalue = hash(file);
					printf("%d\n", hashvalue);

					if(hashvalue == -1)
					{
						printf("\nInvalid file name\n");
						break;
					}

					insert_into_list(hashvalue, file,path);
					break;

		        case 3:printf("Enter the file name to be searched in the hash table\n");
                               scanf("%s",search_file);
				struct list *result;
                                result=(struct list *)malloc(sizeof(struct list));
                                result=serchStartPoint(search_file);
				if(result==NULL)
                                        puts("FILE NOT FOUND");
                                else
                                {      
                                        printf("The file %s is found in the paths \n",search_file);                    
                                        while(result!=NULL)
                                          {
                                                puts(result->pathname);
                                                result=result->next;
                                        }
                               }
			       break;

			case 4: printf("Enter the filename\n");
					scanf("%s", f);
					hashvalue = hash(f);
					printf("Enter the Path\n");
                                        scanf("%s", p);
					int val=deletion(hashvalue,f,p);
					printf("%d",val);
					break;
			case 5:
                                printf("Enter the starting characters for the files to be searched in the hash table\n");
                                scanf("%s",prefix_files);
                                struct list *result_files;
                                result_files=(struct list *)malloc(sizeof(struct list));
                                result_files=search_files(prefix_files);
                                if(result_files==NULL)
                                {
                                        printf("FILE NOT FOUND");
                                       
                                }
                                else{
                                        printf("The files starting with %s are found in the paths \n",prefix_files);
                                        while(result_files!=NULL)
                                          {
                                                printf("File %s in the path %s \n",result_files->filename,result_files->pathname);
                                                result_files=result_files->next;
                                        }
                             
                                }
                                         break;

			case 6: printf("\nProgram terminating\n");
					return 0;
					

		}
	}
}*/
